package leetcode;

public class Day18 {
    public static int[] merge(int[] nums1, int m, int[] nums2, int n) {
        int[] ret=new int[m+n];
        int i=0;
        int j=0;
        int r=0;
        while (i<m&&j<n){
            if (nums1[i]<=nums2[j]){
                ret[r]=nums1[i];
                r++;
                i++;
            }else{
                ret[r]=nums2[j];
                r++;
                j++;
            }
        }
        if(i<m){
            while(i<m){
                ret[r]=nums1[i];
                i++;
                r++;
            }
        }
        if(j<n){
            while(j<n){
                ret[r]=nums2[j];
                j++;
                r++;
            }
        }
        for (int a=0;a<ret.length;a++){
            nums1[a]=ret[a];
        }
        return nums1;
    }
    public int minCost(int[][] costs) {
        int i=costs.length;
        int j=costs[0].length;
        int[][] dp=new int[i][j];
        for (int a=0;a<j;a++){
            dp[0][a]=costs[0][a];
        }
        for (int a=1;a<i;a++){
            for (int b=0;b<j;b++){
                if(b==0){
                    dp[a][b]=Math.min(dp[a-1][1],dp[a-1][2])+costs[a][b];
                }else if(b==2){
                    dp[a][b]=Math.min(dp[a-1][1],dp[a-1][0])+costs[a][b];
                }else {
                    dp[a][b]=Math.min(dp[a-1][0],dp[a-1][2])+costs[a][b];
                }
            }
        }
        return Math.min(Math.min(dp[i-1][0],dp[i-1][1]),dp[i-1][2]);
    }


}
